package com.zh.demo.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.zh.demo.model.Car;
import com.zh.demo.service.CarService;

@Controller
@RequestMapping("car")
@SessionAttributes(names= {"one"})
public class CarController {
	private static Logger logger = Logger.getLogger(CarController.class);

	@Resource
	private CarService cars;
	
	@GetMapping("first")
	@ResponseBody
	public List<Car> home() {
		return cars.show();
		
	}
	@GetMapping("addcar")
	@ResponseBody
	public int add(Car car) {
		return cars.add(car);
		
	}
	
	@RequestMapping("aaa")
	public String fun(Map<String,Object> map) {
		List<Car> str = cars.show();
		
		map.put("one", str);
		return "carhome";
		
	}
	@RequestMapping("add")
	public String getadd() {
		return "add";
	}
	
	@PostMapping("add")
	public String postadd(Car car,Map<String,Object> map) {
		//走数据库添加数据
		cars.add(car);
		//重查数据并存储
        List<Car> str = cars.show();
		//jsp到map拿数据。
		map.put("one", str);
		return "carhome";
//		redirect/
	}
}
